package cn.alcatraz.basic.controller;

import cn.alcatraz.basic.dto.LoginDto;
import cn.alcatraz.basic.exception.BusinessException;
import cn.alcatraz.basic.service.ILoginService;
import cn.alcatraz.basic.utils.AjaxResult;
import cn.alcatraz.user.dto.WechatDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;


    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto){
        try {
            Map<String, Object> map = loginService.account(loginDto);
            return AjaxResult.myself().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
    /**
    * @Title: wechat
    * @Description: 微信登录接口
    * @Author: Alcatraz
    * @Version: 1.0
    * @Date:  2022/7/10 19:46
    * @Parameters: [wechatDto]
    * @Return cn.alcatraz.basic.utils.AjaxResult
    */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WechatDto wechatDto){
        try {
            return loginService.wechat(wechatDto);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    /**
     * @Title: binder
     * @Description: 微信登录绑定账号
     * @Author: Alcatraz
     * @Version: 1.0
     * @Date:  2022/7/10 19:48
     * @Parameters: [wxDto]
     * @Return cn.alcatraz.basic.utils.AjaxResult
     */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WechatDto wechatDto){
        try {
            Map<String, Object> map = loginService.binder(wechatDto);
            return AjaxResult.myself().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.myself().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

}
